package com.goldgov.information.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import us.codecraft.webmagic.selector.Html;

/**
 * 爬虫模板
 * */
public class CrawlerPagingTemplets{

	protected final static Log logger = LogFactory.getLog(CrawlerPagingTemplets.class);
	
	/**
	 * 根据类型运行相关的模板
	 * @throws IOException 
	 * */
	public static List<String> achieveLink(CrawlerPagingTempletEnum crawlerTempletEnum,Html html) throws IOException {
		List<String> pagingLinks=new ArrayList<>();
		switch(crawlerTempletEnum) {
			case URL1:
				logger.info("1===========>开始进行自定义获取分页："+crawlerTempletEnum.name());
				String value1=html.xpath("//div[@class='page']").toString();
				int start1=value1.lastIndexOf("createPageHTML");
				String linkUrl1=value1.substring(start1+1+"createPageHTML".length(), start1+"createPageHTML".length()+2);
				for(int i=1;i<Integer.valueOf(linkUrl1);i++) {
					pagingLinks.add("http://www.ccdi.gov.cn/scdc/zggb/zjsc/index_"+i+".html");
				}
				logger.info(pagingLinks);
				break;
			case URL2:
				logger.info("1===========>开始进行自定义获取分页："+crawlerTempletEnum.name());
				String value2=html.xpath("//div[@class='page']").toString();
				int start2=value2.lastIndexOf("createPageHTML");
				String linkUrl2=value2.substring(start2+1+"createPageHTML".length(), start2+"createPageHTML".length()+2);
				for(int i=1;i<Integer.valueOf(linkUrl2);i++) {
					pagingLinks.add("http://www.nxjjjc.gov.cn/jlsc/qn/index_"+i+".html");
				}
				logger.info(pagingLinks);
				break;
			default:
				break;
		}
		return pagingLinks;
	}
}